/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.resource {
	import org.splink.resource.resourcebundles.LibrariesData;
	import org.splink.resource.resourcebundles.ResourcebundleData;
	import org.splink.resource.resourcebundles.interfaces.IResource;

	/**
	 * <code>ResourceProgressProcessor</code> analyzes a given <code>ResourcebundleData</code>
	 * instance and generates <code>ResourceProgress</code> objects which contain detailed information
	 * regarding the current resource's and the overall resource's load progress.
	 * 
	 * @author Max Kugland
	 */
	internal class ResourceProgressProcessor {
		private var _bundle : ResourcebundleData;
		private var _progess : Array;
		private var _librariesData : LibrariesData;

		public function ResourceProgressProcessor(bundle : ResourcebundleData, librariesData : LibrariesData) {
			_bundle = bundle;
			_librariesData = librariesData;

			analyze();
		}

		private function analyze() : void {
			_progess = new Array();

			var count : int = 0;
			var bytesCount : int = 0;

			var assets : Array = _bundle.assetsData ? _bundle.assetsData.assetDataArray : [];
			var fonts : Array = _bundle.fontsData ? _bundle.fontsData.fontDataArray : [];
			var files : Array = _bundle.filesData ? _bundle.filesData.fileDataArray : [];
			var libraries : Array = _librariesData ? _librariesData.libraryDataArray : [];

			var resources : Array = assets.concat(fonts).concat(files).concat(libraries);
			for each (var res : IResource in resources) {
				if (res.filesize != 0) {
					count++;
					_progess.push(new ResourceProgress(res.id, count, res.filesize, bytesCount));
					bytesCount += res.filesize;
				}
			}

			for each (var progress : ResourceProgress in _progess) {
				progress.setTotalItems(count);
				progress.setTotalBytes(bytesCount);
			}
		}

		/**
		 * Returns a <code>ResourceProgress</code> object which contains detailed information about the
		 * resource loading progress.
		 * 
		 * @param id the id of the item for which a <code>ResourceProgress</code> is returned
		 * @param currentItemBytes the currently loaded bytes of the resource
		 * @return a <code>ResourceProgress</code> object
		 */
		public function getProgressById(id : String, currentItemBytes : int) : ResourceProgress {
			for each (var progress : ResourceProgress in _progess) {
				if (progress.currentId == id) {
					progress.setCurrentLoadedBytes(currentItemBytes);
					return progress;
				}
			}
			return null;
		}

		/**
		 * Finalizes all orphaned objects used by <code>ResourceProgressProcessor</code>
		 */
		public function finalize() : void {
			_bundle = null;
			_progess = null;
		}
	}
}